#include "arraylistwindow.h"
#include <QGridLayout>
#include <QFrame>
#include <cmath>

// InputDialog 实现
InputDialog::InputDialog(const QString &title, const QString &label, QWidget *parent)
    : QDialog(parent)
{
    setupUI(title, label);
}

InputDialog::InputDialog(const QString &title, const QString &label1, const QString &label2, QWidget *parent)
    : QDialog(parent)
{
    setupUI(title, label1, label2);
}

void InputDialog::setupUI(const QString &title, const QString &label1, const QString &label2)
{
    setWindowTitle(title);
    setModal(true);

    QFormLayout *layout = new QFormLayout(this);

    m_firstEdit = new QLineEdit();
    layout->addRow(label1, m_firstEdit);

    if (!label2.isEmpty()) {
        m_secondEdit = new QLineEdit();
        layout->addRow(label2, m_secondEdit);
    } else {
        m_secondEdit = nullptr;
    }

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *okButton = new QPushButton("确定");
    QPushButton *cancelButton = new QPushButton("取消");

    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    layout->addRow(buttonLayout);

    connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
    connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);

    // 根据是否有第二个输入框调整大小
    if (label2.isEmpty()) {
        setFixedSize(300, 120);
    } else {
        setFixedSize(300, 150);
    }
}

QString InputDialog::getFirstValue() const
{
    return m_firstEdit->text();
}

QString InputDialog::getSecondValue() const
{
    if (m_secondEdit) {
        return m_secondEdit->text();
    }
    return "";
}

// ArrayListWindow 实现
ArrayListWindow::ArrayListWindow(QWidget *parent)
    : QMainWindow(parent), m_capacity(20), m_size(0),
    m_animationStep(0), m_animationIndex(-1), m_animationValue(0), m_originalSize(0),
    m_moveFromIndex(-1), m_moveToIndex(-1)
{
    // 分配原生数组内存
    m_data = new int[m_capacity];
    m_originalData = new int[m_capacity];

    resetData();
    m_animationTimer = new QTimer(this);
    connect(m_animationTimer, &QTimer::timeout, this, &ArrayListWindow::onAnimationStep);
    setupUI();
    updateDisplay();
}

ArrayListWindow::~ArrayListWindow()
{
    // 释放动态数组内存
    delete[] m_data;
    delete[] m_originalData;
}

void ArrayListWindow::copyArray(const int* source, int* destination, int count)
{
    for (int i = 0; i < count; i++) {
        destination[i] = source[i];
    }
}

void ArrayListWindow::resetData()
{
    for (int i = 0; i < m_capacity; i++) {
        m_data[i] = 0;
    }
    m_size = 0;
}

void ArrayListWindow::setupUI()
{
    setWindowTitle("顺序表可视化演示");
    setMinimumSize(1400, 800);
    resize(1400, 800);

    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    QHBoxLayout *mainLayout = new QHBoxLayout(centralWidget);

    // 左侧：可视化区域
    QVBoxLayout *leftLayout = new QVBoxLayout();
    leftLayout->setContentsMargins(10, 10, 10, 10);

    QLabel *vizTitle = new QLabel("顺序表可视化");
    vizTitle->setStyleSheet("font-size: 18px; font-weight: bold;");
    leftLayout->addWidget(vizTitle);

    m_scrollArea = new QScrollArea();
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    m_visualizationWidget = new QWidget();
    m_visualizationWidget->setMinimumSize(1000, 600);

    m_scrollArea->setWidget(m_visualizationWidget);
    leftLayout->addWidget(m_scrollArea);

    // 初始化数据输入
    QGroupBox *inputGroup = new QGroupBox("初始化数据");
    QHBoxLayout *inputLayout = new QHBoxLayout(inputGroup);
    m_inputEdit = new QLineEdit();
    m_inputEdit->setPlaceholderText("输入元素，用空格分隔 (如: 1 2 3 4 5)");
    QPushButton *inputBtn = new QPushButton("初始化");
    inputLayout->addWidget(m_inputEdit);
    inputLayout->addWidget(inputBtn);
    leftLayout->addWidget(inputGroup);

    mainLayout->addLayout(leftLayout, 4); // 左侧占4份宽度

    // 右侧：操作按钮和信息显示
    QVBoxLayout *rightLayout = new QVBoxLayout();
    rightLayout->setContentsMargins(10, 10, 10, 10);

    // 返回按钮
    QPushButton *returnBtn = new QPushButton("返回主菜单");
    rightLayout->addWidget(returnBtn);

    // 操作按钮组
    QGroupBox *opsGroup = new QGroupBox("操作");
    QVBoxLayout *opsLayout = new QVBoxLayout(opsGroup);

    QPushButton *insertBtn = new QPushButton("插入元素");
    QPushButton *removeBtn = new QPushButton("删除元素");
    QPushButton *searchBtn = new QPushButton("查找元素");
    QPushButton *clearBtn = new QPushButton("清空顺序表");

    opsLayout->addWidget(insertBtn);
    opsLayout->addWidget(removeBtn);
    opsLayout->addWidget(searchBtn);
    opsLayout->addWidget(clearBtn);

    rightLayout->addWidget(opsGroup);

    // 信息显示
    QGroupBox *infoGroup = new QGroupBox("顺序表信息");
    QVBoxLayout *infoLayout = new QVBoxLayout(infoGroup);

    m_display = new QTextEdit();
    m_display->setReadOnly(true);
    m_display->setMaximumHeight(400);
    infoLayout->addWidget(m_display);

    rightLayout->addWidget(infoGroup);

    mainLayout->addLayout(rightLayout, 1); // 右侧占1份宽度

    // 连接信号和槽
    connect(inputBtn, &QPushButton::clicked, this, &ArrayListWindow::onInputData);
    connect(insertBtn, &QPushButton::clicked, this, &ArrayListWindow::onInsert);
    connect(removeBtn, &QPushButton::clicked, this, &ArrayListWindow::onRemove);
    connect(searchBtn, &QPushButton::clicked, this, &ArrayListWindow::onSearch);
    connect(clearBtn, &QPushButton::clicked, this, &ArrayListWindow::onClear);
    connect(returnBtn, &QPushButton::clicked, this, &ArrayListWindow::onReturnToMainMenu);
}

void ArrayListWindow::onReturnToMainMenu()
{
    emit returnToMainMenu();
    this->close();
}

void ArrayListWindow::updateDisplay()
{
    QString info = QString("顺序表 (大小: %1/%2)\n\n").arg(m_size).arg(m_capacity);
    info += "内容: ";
    for (int i = 0; i < m_size; i++) {
        info += QString::number(m_data[i]) + " ";
    }
    info += "\n\n索引: ";
    for (int i = 0; i < m_size; i++) {
        info += QString::number(i) + " ";
    }
    m_display->setText(info);

    visualizeDataStructure();
}

void ArrayListWindow::appendToInfo(const QString &text)
{
    QString currentText = m_display->toPlainText();
    m_display->setText(currentText + "\n" + text);
}

void ArrayListWindow::visualizeDataStructure()
{
    updateVisualizationWithHighlight();
}

void ArrayListWindow::updateVisualizationWithHighlight(int highlightIndex, int newValueIndex, int searchIndex, int moveFromIndex, int moveToIndex)
{
    QLayout *layout = m_visualizationWidget->layout();
    if (layout) {
        QLayoutItem *item;
        while ((item = layout->takeAt(0)) != nullptr) {
            delete item->widget();
            delete item;
        }
        delete layout;
    }

    QGridLayout *vizLayout = new QGridLayout(m_visualizationWidget);
    vizLayout->setHorizontalSpacing(10);
    vizLayout->setVerticalSpacing(10);

    int itemsPerRow = 10;

    for (int i = 0; i < m_capacity; i++) {
        QFrame *frame = new QFrame();
        frame->setFrameStyle(QFrame::Box);
        frame->setLineWidth(2);
        frame->setMinimumSize(80, 80);

        // 设置不同的颜色来突出显示
        if (i == highlightIndex) {
            frame->setStyleSheet("QFrame { background-color: #ffff99; border: 3px solid #ff6600; }");
        } else if (i == newValueIndex) {
            frame->setStyleSheet("QFrame { background-color: #99ff99; border: 3px solid #00cc00; }");
        } else if (i == searchIndex) {
            frame->setStyleSheet("QFrame { background-color: #ffcc99; border: 3px solid #ff9900; }"); // 查找高亮
        } else if (i == moveFromIndex) {
            frame->setStyleSheet("QFrame { background-color: #ff9999; border: 3px solid #ff0000; }"); // 移动源位置高亮
        } else if (i == moveToIndex) {
            frame->setStyleSheet("QFrame { background-color: #9999ff; border: 3px solid #0000ff; }"); // 移动目标位置高亮
        } else if (i < m_size) {
            frame->setStyleSheet("QFrame { background-color: #e6f3ff; }");
        } else {
            frame->setStyleSheet("QFrame { background-color: #f0f0f0; }");
        }

        QVBoxLayout *frameLayout = new QVBoxLayout(frame);

        QLabel *indexLabel = new QLabel("索引: " + QString::number(i));
        indexLabel->setAlignment(Qt::AlignCenter);
        indexLabel->setStyleSheet("color: #666666; font-size: 10px;");

        QLabel *valueLabel;
        if (i < m_size) {
            valueLabel = new QLabel(QString::number(m_data[i]));
            valueLabel->setStyleSheet("color: #0066cc;");
        } else {
            valueLabel = new QLabel("空");
            valueLabel->setStyleSheet("color: #999999;");
        }

        valueLabel->setAlignment(Qt::AlignCenter);
        QFont valueFont = valueLabel->font();
        valueFont.setPointSize(16);
        valueFont.setBold(true);
        valueLabel->setFont(valueFont);

        frameLayout->addWidget(indexLabel);
        frameLayout->addWidget(valueLabel);

        int row = i / itemsPerRow;
        int col = i % itemsPerRow;
        vizLayout->addWidget(frame, row, col);
    }

    m_visualizationWidget->setLayout(vizLayout);
    m_visualizationWidget->adjustSize();
}

void ArrayListWindow::startInsertAnimation(int index, int value)
{
    // 保存原始状态 - 使用原生数组拷贝
    copyArray(m_data, m_originalData, m_capacity);
    m_originalSize = m_size;

    m_animationIndex = index;
    m_animationValue = value;
    m_animationStep = 0;
    m_animationType = "insert";

    appendToInfo(QString("\n=== 开始插入操作 ==="));
    appendToInfo(QString("准备在位置 %1 插入值 %2").arg(index).arg(value));

    m_animationTimer->start(1000);  // 每1000ms执行一步动画
}

void ArrayListWindow::startRemoveAnimation(int value)
{
    // 先检查元素是否存在
    bool found = false;
    for (int i = 0; i < m_size; i++) {
        if (m_data[i] == value) {
            found = true;
            break;
        }
    }

    if (!found) {
        QMessageBox::information(this, "删除失败", QString("未找到元素 %1!").arg(value));
        return;
    }

    // 保存原始状态 - 使用原生数组拷贝
    copyArray(m_data, m_originalData, m_capacity);
    m_originalSize = m_size;

    m_animationValue = value;
    m_animationStep = 0;
    m_animationType = "remove";

    appendToInfo(QString("\n=== 开始删除操作 ==="));
    appendToInfo(QString("准备删除值 %1").arg(value));

    m_animationTimer->start(800);  // 每800ms执行一步动画
}

void ArrayListWindow::startSearchAnimation(int value)
{
    m_animationValue = value;
    m_animationStep = 0;
    m_animationType = "search";

    appendToInfo(QString("\n=== 开始查找操作 ==="));
    appendToInfo(QString("准备查找值 %1").arg(value));

    m_animationTimer->start(800);  // 每800ms执行一步动画
}

void ArrayListWindow::onAnimationStep()
{
    if (m_animationType == "insert") {
        if (m_animationStep == 0) {
            // 步骤1：合法性校验
            appendToInfo("步骤1: 进行插入前的合法性校验");
            appendToInfo(QString("  检查插入位置 %1 是否在有效范围内 [0, %2]").arg(m_animationIndex).arg(m_originalSize));

            if (m_animationIndex < 0 || m_animationIndex > m_originalSize) {
                appendToInfo("  ✗ 插入位置无效！插入位置必须在 0 到当前元素个数之间");
                m_animationTimer->stop();
                QMessageBox::warning(this, "插入失败",
                                     QString("插入位置 %1 无效！必须在 0 到 %2 之间").arg(m_animationIndex).arg(m_originalSize));
                return;
            } else {
                appendToInfo("  ✓ 插入位置有效");
            }

            appendToInfo(QString("  检查顺序表容量 (%1/%2)").arg(m_originalSize).arg(m_capacity));
            if (m_originalSize >= m_capacity) {
                appendToInfo("  ✗ 顺序表已满，无法插入新元素");
                m_animationTimer->stop();
                QMessageBox::warning(this, "插入失败", "顺序表已满，无法插入新元素");
                return;
            } else {
                appendToInfo("  ✓ 顺序表还有剩余容量");
            }

            appendToInfo("  ✓ 所有合法性校验通过，可以开始插入操作");
            updateVisualizationWithHighlight(m_animationIndex);
        }
        else if (m_animationStep == 1) {
            // 步骤2：准备向后移位
            appendToInfo(QString("步骤2: 准备向后移位操作"));
            appendToInfo(QString("  从位置 %1 开始的所有元素需要向后移动一位").arg(m_animationIndex));
            appendToInfo(QString("  移位必须从最后一个有效元素开始，依次向后移动"));
            updateVisualizationWithHighlight(m_animationIndex);
        }
        else if (m_animationStep >= 2 && m_animationStep <= 2 + (m_originalSize - m_animationIndex)) {
            // 步骤3及以后：向后移位过程
            int shiftStep = m_animationStep - 2;
            int fromIndex = m_originalSize - 1 - shiftStep;
            int toIndex = m_originalSize - shiftStep;

            // 如果是第一步移位，需要先扩展数组大小
            if (shiftStep == 0) {
                m_size = m_originalSize + 1;
                appendToInfo(QString("步骤3: 扩展顺序表大小，准备移位"));
            }

            if (fromIndex >= m_animationIndex) {
                appendToInfo(QString("步骤%1: 将位置 %2 的元素 %3 移动到位置 %4")
                                 .arg(m_animationStep + 1)
                                 .arg(fromIndex)
                                 .arg(m_originalData[fromIndex])
                                 .arg(toIndex));

                // 执行移动
                m_data[toIndex] = m_originalData[fromIndex];

                // 如果是最后一步移位，清空原位置的值
                if (fromIndex == m_animationIndex) {
                    m_data[fromIndex] = 0; // 清空原位置，为插入做准备
                }

                // 高亮显示移动的源位置和目标位置
                updateVisualizationWithHighlight(-1, -1, -1, fromIndex, toIndex);
            }
        }
        else if (m_animationStep == 3 + (m_originalSize - m_animationIndex)) {
            // 步骤：插入新元素
            appendToInfo(QString("步骤%1: 在位置 %2 插入新元素 %3")
                             .arg(m_animationStep + 1)
                             .arg(m_animationIndex)
                             .arg(m_animationValue));
            appendToInfo("  现在位置已经空出，可以直接插入新元素");

            // 实际插入新元素
            m_data[m_animationIndex] = m_animationValue;

            updateVisualizationWithHighlight(-1, m_animationIndex);
        }
        else if (m_animationStep == 4 + (m_originalSize - m_animationIndex)) {
            // 步骤：更新顺序表长度
            appendToInfo(QString("步骤%1: 更新顺序表的有效长度")
                             .arg(m_animationStep + 1));
            appendToInfo(QString("  顺序表大小已更新: %1 -> %2").arg(m_originalSize).arg(m_size));
            appendToInfo("  这一步确保后续操作只识别真正的有效元素");

            updateVisualizationWithHighlight(-1, m_animationIndex);
        }
        else if (m_animationStep == 5 + (m_originalSize - m_animationIndex)) {
            // 动画完成
            m_animationTimer->stop();
            appendToInfo("插入完成!");

            // 确保未使用的位置被正确显示
            for (int i = m_size; i < m_capacity; i++) {
                m_data[i] = 0;
            }

            updateDisplay();
            QMessageBox::information(this, "插入成功",
                                     QString("元素 %1 已成功插入到位置 %2").arg(m_animationValue).arg(m_animationIndex));
            return;
        }
    }
    else if (m_animationType == "remove") {
        if (m_animationStep == 0) {
            appendToInfo(QString("步骤1: 开始查找要删除的元素 %1").arg(m_animationValue));
            updateVisualizationWithHighlight();
        }
        else if (m_animationStep <= m_originalSize) {
            int currentIndex = m_animationStep - 1;
            if (currentIndex < m_originalSize) {
                appendToInfo(QString("步骤%1: 检查位置 %2 的元素 %3").arg(m_animationStep+1).arg(currentIndex).arg(m_originalData[currentIndex]));

                if (m_originalData[currentIndex] == m_animationValue) {
                    appendToInfo(QString("找到要删除的元素在位置 %1").arg(currentIndex));
                    updateVisualizationWithHighlight(currentIndex);

                    // 标记找到元素，下一步开始删除
                    m_animationIndex = currentIndex;
                    m_animationStep = m_originalSize + 1; // 跳到删除步骤
                } else {
                    updateVisualizationWithHighlight(currentIndex);
                }
            }
        }
        else if (m_animationStep == m_originalSize + 1) {
            if (m_animationIndex != -1) {
                // 统一处理删除操作，不再区分尾部和中间删除
                appendToInfo(QString("步骤%1: 准备删除位置 %2 的元素").arg(m_animationStep+1).arg(m_animationIndex));

                if (m_animationIndex < m_originalSize - 1) {
                    // 中间删除：需要向前移位
                    appendToInfo("需要向前移位覆盖被删除的元素");
                } else {
                    // 尾部删除：直接删除，无需移位
                    appendToInfo("删除的是尾部元素，无需移位");
                }

                updateVisualizationWithHighlight(m_animationIndex);
            } else {
                appendToInfo("未找到要删除的元素");
                m_animationTimer->stop();
                QMessageBox::information(this, "删除失败", QString("未找到元素 %1!").arg(m_animationValue));
                return;
            }
        }
        else if (m_animationStep >= m_originalSize + 2 &&
                 m_animationStep <= m_originalSize + 1 + (m_originalSize - m_animationIndex - 1)) {
            // 统一的向前移位过程
            int shiftStep = m_animationStep - (m_originalSize + 2);
            int fromIndex = m_animationIndex + shiftStep + 1;
            int toIndex = m_animationIndex + shiftStep;

            if (fromIndex < m_originalSize) {
                appendToInfo(QString("步骤%1: 将位置 %2 的元素 %3 移动到位置 %4")
                                 .arg(m_animationStep+1)
                                 .arg(fromIndex)
                                 .arg(m_originalData[fromIndex])
                                 .arg(toIndex));

                // 执行移动
                m_data[toIndex] = m_originalData[fromIndex];

                // 高亮显示移动的源位置和目标位置
                updateVisualizationWithHighlight(-1, -1, -1, fromIndex, toIndex);
            }

            // 如果是最后一步移动，更新大小
            if (fromIndex == m_originalSize - 1 || m_animationIndex == m_originalSize - 1) {
                m_size = m_originalSize - 1;
            }
        }
        else if (m_animationStep == m_originalSize + 2 + (m_originalSize - m_animationIndex - 1)) {
            // 对于尾部删除，如果之前没有更新大小，在这里更新
            if (m_animationIndex == m_originalSize - 1 && m_size == m_originalSize) {
                m_size = m_originalSize - 1;
            }

            // 动画完成
            m_animationTimer->stop();
            appendToInfo("删除完成!");
            appendToInfo(QString("顺序表大小已更新: %1 -> %2").arg(m_originalSize).arg(m_size));

            // 确保未使用的位置被正确显示
            for (int i = m_size; i < m_capacity; i++) {
                m_data[i] = 0;
            }

            updateDisplay();
            QMessageBox::information(this, "删除成功", QString("元素 %1 已删除").arg(m_animationValue));
            return;
        }
    }
    else if (m_animationType == "search") {
        if (m_animationStep == 0) {
            appendToInfo(QString("步骤1: 开始顺序查找元素 %1").arg(m_animationValue));
            updateVisualizationWithHighlight();
        }
        else if (m_animationStep <= m_size) {
            int currentIndex = m_animationStep - 1;
            if (currentIndex < m_size) {
                appendToInfo(QString("步骤%1: 检查位置 %2 的元素 %3").arg(m_animationStep+1).arg(currentIndex).arg(m_data[currentIndex]));

                if (m_data[currentIndex] == m_animationValue) {
                    appendToInfo(QString("找到元素在位置 %1").arg(currentIndex));
                    updateVisualizationWithHighlight(-1, -1, currentIndex);
                    m_animationTimer->stop();
                    QMessageBox::information(this, "查找结果", QString("元素 %1 在索引 %2 处").arg(m_animationValue).arg(currentIndex));
                    return;
                } else {
                    updateVisualizationWithHighlight(-1, -1, currentIndex);
                }
            }
        }
        else {
            // 查找完成，未找到
            appendToInfo("未找到指定元素");
            updateVisualizationWithHighlight(); // 清除高亮
            m_animationTimer->stop();
            QMessageBox::information(this, "查找结果", QString("未找到元素 %1!").arg(m_animationValue));
            return;
        }
    }

    m_animationStep++;
}

void ArrayListWindow::onInputData()
{
    QString input = m_inputEdit->text();
    if (input.isEmpty()) return;

    QStringList numbers = input.split(' ', Qt::SkipEmptyParts);
    m_size = 0;

    for (const QString& numStr : numbers) {
        if (m_size >= m_capacity) {
            QMessageBox::warning(this, "容量已满",
                                 QString("顺序表容量为%1，只初始化了前%1个元素").arg(m_capacity));
            break;
        }

        bool ok;
        int value = numStr.toInt(&ok);
        if (ok) {
            m_data[m_size++] = value;
        } else {
            QMessageBox::warning(this, "输入错误",
                                 QString("'%1' 不是有效的整数，已跳过").arg(numStr));
        }
    }

    for (int i = m_size; i < m_capacity; i++) {
        m_data[i] = 0;
    }

    updateDisplay();
    appendToInfo("=== 数据初始化完成 ===");
    m_inputEdit->clear();
}

void ArrayListWindow::onInsert()
{
    InputDialog dialog("插入元素", "插入位置:", "插入的值:", this);
    if (dialog.exec() == QDialog::Accepted) {
        QString indexInput = dialog.getFirstValue();
        QString valueInput = dialog.getSecondValue();

        if (indexInput.isEmpty() || valueInput.isEmpty()) {
            QMessageBox::warning(this, "输入错误", "请同时输入插入位置和插入值");
            return;
        }

        bool indexOk, valueOk;
        int index = indexInput.toInt(&indexOk);
        int value = valueInput.toInt(&valueOk);

        if (!indexOk || !valueOk) {
            QMessageBox::warning(this, "输入错误", "请输入有效的整数");
            return;
        }

        // 提前进行基本的合法性检查，避免启动动画后立即失败
        if (index < 0 || index > m_size) {
            QMessageBox::warning(this, "位置错误",
                                 QString("插入位置必须在 0 到 %1 之间").arg(m_size));
            return;
        }

        if (m_size >= m_capacity) {
            QMessageBox::warning(this, "插入失败", "顺序表已满，无法插入新元素");
            return;
        }

        startInsertAnimation(index, value);
    }
}

void ArrayListWindow::onRemove()
{
    InputDialog dialog("删除元素", "要删除的元素:", this);
    if (dialog.exec() == QDialog::Accepted) {
        QString input = dialog.getFirstValue();
        if (input.isEmpty()) return;

        bool ok;
        int value = input.toInt(&ok);
        if (ok) {
            startRemoveAnimation(value);
        } else {
            QMessageBox::warning(this, "输入错误", "请输入有效的整数");
        }
    }
}

void ArrayListWindow::onSearch()
{
    InputDialog dialog("查找元素", "要查找的元素:", this);
    if (dialog.exec() == QDialog::Accepted) {
        QString input = dialog.getFirstValue();
        if (input.isEmpty()) return;

        bool ok;
        int value = input.toInt(&ok);
        if (ok) {
            startSearchAnimation(value);
        } else {
            QMessageBox::warning(this, "输入错误", "请输入有效的整数");
        }
    }
}

void ArrayListWindow::onClear()
{
    resetData();
    updateDisplay();
    appendToInfo("=== 顺序表已清空 ===");
    QMessageBox::information(this, "清空成功", "顺序表已清空");
}
